package com.zhangyongyi.carhome.fragment.person;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.zhangyongyi.carhome.R;
import com.zhangyongyi.carhome.tools.DayNightMode;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qq.QQ;

/**
 * Created by dllo on 16/1/7.
 * 我
 */
public class PersonFragment extends Fragment implements View.OnClickListener {


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_person, null);
        //夜间模式
        v.findViewById(R.id.iv_yj).setOnClickListener(this);
        v.findViewById(R.id.personhobby_Tv).setOnClickListener(this);
        v.findViewById(R.id.personWeiBoLogin_Tv).setOnClickListener(this);
        v.findViewById(R.id.personQQLogin_Tv).setOnClickListener(this);
        return v;
    }

    @Override
    public void onClick(View v) {
        int theme = DayNightMode.getDayNightMode(getActivity());
        switch (v.getId()) {
            case R.id.iv_yj:
                if (theme == DayNightMode.THEME_SUN) {
                    DayNightMode.setDayNightMode(getActivity(), DayNightMode.THEME_NIGHT);
                } else {
                    DayNightMode.setDayNightMode(getActivity(), DayNightMode.THEME_SUN);
                }
                // 注意改过主题后一定要，把activity finish在重开一遍，因为更改主题只能在oncreat中进行
//                getActivity().finish();
                getActivity().startActivity(new Intent(getActivity(), getActivity().getClass()));
                break;

            case R.id.personhobby_Tv:
                showShare();
                break;

            case R.id.personQQLogin_Tv:


                PlatformActionListener listenerqq = new PlatformActionListener() {
                    @Override
                    public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                        //授权成功
                        String id = platform.getDb().getUserId();
                        String name = platform.getDb().getUserName();
                        String imageUrl = platform.getDb().getUserIcon();
                        Log.d("message", "id:" + id + "name:" + name + "url:" + imageUrl);
                        Log.d("hashmap=======", "" + hashMap);
                        Toast.makeText(getActivity(), "登陆成功", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onError(Platform platform, int i, Throwable throwable) {
                        //授权失败
                    }

                    @Override
                    public void onCancel(Platform platform, int i) {
                        //取消授权
                    }
                };
                Platform qq = ShareSDK.getPlatform(getActivity(), QQ.NAME);

                qq.setPlatformActionListener(listenerqq);
                qq.showUser(null);
                qq.authorize();


                break;

            case R.id.personWeiBoLogin_Tv:
                //第三方登录

                PlatformActionListener listener = new PlatformActionListener() {
                    @Override
                    public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                        //授权成功
                        String id = platform.getDb().getUserId();
                        String name = platform.getDb().getUserName();
                        String imageUrl = platform.getDb().getUserIcon();
                        Log.d("message", "id:" + id + "name:" + name + "url:" + imageUrl);
                        Log.d("hashmap=======", "" + hashMap);
                        Toast.makeText(getActivity(), "登陆成功", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onError(Platform platform, int i, Throwable throwable) {
                        //授权失败
                    }

                    @Override
                    public void onCancel(Platform platform, int i) {
                        //取消授权
                    }
                };
                Platform sina = ShareSDK.getPlatform(getActivity(), SinaWeibo.NAME);

                sina.setPlatformActionListener(listener);
                sina.showUser(null);
                sina.authorize();


                break;


        }

    }

    private void showShare() {
        ShareSDK.initSDK(getActivity());
        OnekeyShare oks = new OnekeyShare();
        //关闭sso授权
        oks.disableSSOWhenAuthorize();
// 分享时Notification的图标和文字  2.5.9以后的版本不调用此方法
        //oks.setNotification(R.drawable.ic_launcher, getString(R.string.app_name));
        // title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间使用
        oks.setTitle("永毅测试");
        // titleUrl是标题的网络链接，仅在人人网和QQ空间使用
        oks.setTitleUrl("http://sharesdk.cn");
        // text是分享文本，所有平台都需要这个字段
        oks.setText("我是分享文本，啦啦啦~");
        // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
        //    oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
        // url仅在微信（包括好友和朋友圈）中使用
        oks.setUrl("http://sharesdk.cn");
        // comment是我对这条分享的评论，仅在人人网和QQ空间使用
        oks.setComment("我是测试评论文本");
        // site是分享此内容的网站名称，仅在QQ空间使用
        oks.setSite(getString(R.string.app_name));
        // siteUrl是分享此内容的网站地址，仅在QQ空间使用
        oks.setSiteUrl("http://sharesdk.cn");
// 启动分享GUI
        oks.show(getActivity());
    }
}
